/*
import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { useUserStore } from './user'
import { insertCartAPI, findNewCartListAPI, delCartAPI } from '@/apis/cart'

export const useCartStore = defineStore('cart', () => {
    const userStore = useUserStore()
    const isLogin = computed(() => userStore.userInfo.token)
    //1.定义state - cartList
    const cartList = ref([])
    // 2. 定义action - addCart
    const addCart = async (goods) => {
        const { skuId, count } = goods
        if (isLogin.value) {
            // 登录之后的加入购车逻辑
            await insertCartAPI({ skuId, count })
            updateNewList()
        } else {
            // 添加购物车操作
            // 已添加过 - count + 1
            // 没有添加过 - 直接push
            // 思路：通过匹配传递过来的商品对象中的skuId能不能在cartList中找到，找到了就是添加过
            const item = cartList.value.find((item) => goods.skuId === item.skuId)
            if (item) {
                // 找到了
                item.count++
            } else {
                // 没找到
                cartList.value.push(goods)
            }
        }
    }

    //清除购物车
    const clearCart = () => {
        cartList.value = []
    }
    // 删除购物车
    const delCart = async (skuId) => {
        if (isLogin.value) {
            // 调用接口实现接口购物车中的删除功能
            await delCartAPI([skuId])
            updateNewList()
        } else {
            // 思路：
            // 1. 找到要删除项的下标值 - splice
            // 2. 使用数组的过滤方法 - filter
            const idx = cartList.value.findIndex((item) => skuId === item.skuId)
            cartList.value.splice(idx, 1)
        }
    }
    const updateNewList = async () => {
        const res = await findNewCartListAPI()
        cartList.value = res.result
    }
    // 单选功能
    const singleCheck = (skuId, selected) => {
        // 通过skuId找到要修改的那一项 然后把它的selected修改为传过来的selected
        const item = cartList.value.find((item) => item.skuId === skuId)
        item.selected = selected
    }
    // 全选功能action
    const allCheck = (selected) => {
        // 把cartList中的每一项的selected都设置为当前的全选框状态
        cartList.value.forEach(item => item.selected = selected)
    }
    // 是否全选计算属性
    const isAll = computed(() => cartList.value.every((item) => item.selected))
    // 计算属性
    // 1. 总的数量 所有项的count之和
    const allCount = computed(() => cartList.value.reduce((a, c) => a + c.count, 0))
    // 2. 总价 所有项的count*price之和
    const allPrice = computed(() => cartList.value.reduce((a, c) => a + c.count * c.price, 0))
    // 3. 已选择数量
    const selectedCount = computed(() => cartList.value.filter(item => item.selected).reduce((a, c) => a + c.count, 0))
    // 4. 已选择商品价钱合计
    const selectedPrice = computed(() => cartList.value.filter(item => item.selected).reduce((a, c) => a + c.count * c.price, 0))
    return {
        cartList,
        allCount,
        allPrice,
        isAll,
        selectedCount,
        selectedPrice,
        addCart,
        delCart,
        singleCheck,
        allCheck,
        clearCart,
        updateNewList
    }
    

},

 {
    persist: true
 
 }
 
)
 */
import {defineStore} from "pinia";
import { ref, computed } from 'vue'
import {useUserStore} from "@/stores/user.js";

import {delCartAPI, findNewCartListAPI, insertCartAPI,updateCartItem} from "@/apis/cart.js";
export const useCartStore = defineStore(
    'cart',
    ()=>{
        const cartList = ref([]);
        const userStore =  useUserStore()
        const islogin = computed(()=>userStore.userInfo.token)
        //通过token值判断是否登录， 如果登录过就有token值
        //获取登录后的最新购物车列表action
        const updateLoginCartList = async ()=>{
            const res = await findNewCartListAPI()
            cartList.value = res.result
            //获得最新的数据
        }
        const addCart = async (goods) => {
            if(islogin.value){
                await insertCartAPI(goods);
                await updateLoginCartList();


            }else {
                //判断商品是否在购物车
                const findItem = cartList.value.find(item => goods.skuId === item.skuId);
                if (findItem) {
                    findItem.count += goods.count;
                } else {
                    cartList.value.push(goods)
                }
            }
        }
        const delCart = async (skuId)=>{
            if(islogin.value){
                await delCartAPI([skuId]);
                await updateLoginCartList();
                //获取的最新的数据


            }
            const index = cartList.value.findIndex(item=>skuId === item.skuId)
            cartList.value.splice(index,1)
        }
        //全选功能
        const checkAll = (selected) =>{
            cartList.value.forEach(item => item.selected = selected)
        }
        //清除购物车
        const clearCart = ()=> {
            cartList.value = []

        }
        //修改购物项

        const updateCart = async (goods) => {
            const {skuId,count,selected} = goods
            if (islogin.value) {
                await updateCartItem(skuId,{count,selected})
            }

        }
        //是否全选的计算属性
        const isAll = computed(()=> cartList.value.every((item=>item.selected)))
        // 1. 总的数量 所有项的count之和
        const allCount = computed(() => cartList.value.reduce((a, c) => a + c.count, 0))
        // 2. 总价 所有项的count*price之和
        const allPrice = computed(() => cartList.value.reduce((a, c) => a + c.count * c.price, 0))

        //已选择的数量
        const selectedCount = computed(()=>cartList.value.filter(item=>item.selected)
            .reduce((a,c) => a + c.count,0))

        //已选择的商品价钱合计
        const selectedPrice = computed(()=>cartList.value.filter(item=>item.selected).reduce((a,c)=> a + c.count * c.price,0))
        return{
            cartList,
            allPrice,
            allCount,
            addCart,
            delCart,
            checkAll,
            isAll,
            selectedCount,
            selectedPrice,
            updateLoginCartList,
            clearCart,
            updateCart

        }


    },
    {
        persist:true
    }
)